﻿#include "DAMessageLogItem.h"
//===================================================
// using DA namespace -- 禁止在头文件using！！
//===================================================

using namespace DA;

//===================================================
// DAMessageLogItem
//===================================================
DAMessageLogItem::DAMessageLogItem()
    : m_validFlag(false), m_msgType(QtDebugMsg), m_msg(""), m_datetime(QDateTime::currentDateTime())
{
    m_fileName     = "";
    m_functionName = "";
    m_line         = -1;
}

DAMessageLogItem::DAMessageLogItem(QtMsgType type, const QMessageLogContext& context, const QString& m)
    : m_validFlag(true), m_msgType(type), m_msg(m), m_datetime(QDateTime::currentDateTime())
{
    m_fileName     = context.file ? context.file : "";
    m_functionName = (context.function ? context.function : "");
    m_line         = context.line;
}

DAMessageLogItem::DAMessageLogItem(const DAMessageLogItem& i)
{
    operator=(i);
}

DAMessageLogItem& DAMessageLogItem::operator=(const DAMessageLogItem& i)
{
    m_validFlag      = i.m_validFlag;
    m_msgType        = i.m_msgType;
    m_msg            = i.m_msg;
    m_datetime       = i.m_datetime;
    m_fileName       = i.m_fileName;
    m_functionName   = i.m_functionName;
    m_line           = i.m_line;
    m_datetimeString = i.m_datetimeString;
    m_msgLocal8Bit.reset();
    m_fileLocal8Bit.reset();
    m_datetimeLocal8Bit.reset();
    m_funcitonLocal8Bit.reset();
    return *this;
}

bool DAMessageLogItem::isValid() const
{
    return m_validFlag;
}

QString DAMessageLogItem::datetimeToString(bool showms) const
{
    return m_datetime.toString(showms ? "yyyy-MM-dd HH:mm:ss.zzz" : "yyyy-MM-dd HH:mm:ss");
}

const QString& DAMessageLogItem::getMsg() const
{
    return m_msg;
}

const QString& DAMessageLogItem::getFileName() const
{
    return m_fileName;
}

const QString& DAMessageLogItem::getFunctionName() const
{
    return m_functionName;
}

QtMsgType DAMessageLogItem::getMsgType() const
{
    return m_msgType;
}

int DAMessageLogItem::getLine() const
{
    return m_line;
}

const QDateTime& DAMessageLogItem::getDateTime() const
{
    return m_datetime;
}

const char* DAMessageLogItem::datetimeChar() const
{
    if (!m_datetimeLocal8Bit) {
#if __cplusplus >= 201402L
        m_datetimeLocal8Bit = std::make_unique< QByteArray >(datetimeToString().toLocal8Bit());
#else
        m_datetimeLocal8Bit.reset(new QByteArray(datetimeToString().toLocal8Bit()));
#endif
    }
    return (m_datetimeLocal8Bit->constData());
}

const char* DAMessageLogItem::msgChar() const
{
    if (!m_msgLocal8Bit) {
#if __cplusplus >= 201402L
        m_msgLocal8Bit = std::make_unique< QByteArray >(m_msg.toLocal8Bit());
#else
        m_msgLocal8Bit.reset(new QByteArray(m_msg.toLocal8Bit()));
#endif
    }
    return (m_msgLocal8Bit->constData());
}

const char* DAMessageLogItem::fileChar() const
{
    if (!m_fileLocal8Bit) {
#if __cplusplus >= 201402L
        m_fileLocal8Bit = std::make_unique< QByteArray >(m_fileName.toLocal8Bit());
#else
        m_fileLocal8Bit.reset(new QByteArray(m_fileName.toLocal8Bit()));
#endif
    }
    return (m_fileLocal8Bit->constData());
}

const char* DAMessageLogItem::functionChar() const
{
    if (!m_funcitonLocal8Bit) {
#if __cplusplus >= 201402L
        m_funcitonLocal8Bit = std::make_unique< QByteArray >(m_functionName.toLocal8Bit());
#else
        m_funcitonLocal8Bit.reset(new QByteArray(m_functionName.toLocal8Bit()));
#endif
    }
    return (m_funcitonLocal8Bit->constData());
}
